package com.palm.hubo2o;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.ColorRes;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.AppCompatDelegate;
import android.support.v7.widget.Toolbar;

import com.palm.hubo2o.ui.MainActivity;
import com.palm.hubo2o.util.AppConfig;
import com.palm.palmutils.ColorUtils;
import com.umeng.analytics.MobclickAgent;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by dest1 on 2015/9/1.
 */
public abstract class BaseActivity extends AppCompatActivity {
    private final String TAG = this.getClass().getName();
    @Bind(R.id.toolbar)
    @Nullable
    protected Toolbar toolbar;

    static {
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ((PalmApplication) getApplication()).registerActivity(this);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        ((PalmApplication) getApplication()).unregisterActivity(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        ButterKnife.bind(this);
        if (toolbar != null) {
            setSupportActionBar(toolbar);
            if (!(this instanceof MainActivity))
                getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }


    protected int getColorRGB(@ColorRes int id) {
        return ContextCompat.getColor(this, id);
    }

    protected void changeWindowColor(int rgb) {
        if (toolbar != null)
            toolbar.setBackgroundColor(rgb);
        if (AppConfig.ANDROID_VERSION >= 21) {
            getWindow().setStatusBarColor(ColorUtils.darkerColor(rgb));
        }
    }

    protected Context getContext() {
        return this;
    }
}
